package com.xipu.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xipu.yygh.enums.RefundStatusEnum;
import com.xipu.yygh.model.order.PaymentInfo;
import com.xipu.yygh.model.order.RefundInfo;
import com.xipu.yygh.order.mapper.RefundInfoMapper;
import com.xipu.yygh.order.service.RefundInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 退款信息表 服务实现类
 * </p>
 *
 * @author gql
 * @since 2022-05-15
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        // 1. 查询退款记录是否存在,若存在直接返回
        RefundInfo refundInfo = this.baseMapper.selectOne(Wrappers.lambdaQuery(RefundInfo.class)
                .eq(RefundInfo::getOrderId, paymentInfo.getOrderId()));
        if (!ObjectUtils.isEmpty(refundInfo)) {
            return refundInfo;
        }
        // 2.若不存在则保存退款记录
        refundInfo = new RefundInfo();
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        this.baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
